let requestUrl;

function addHandler() {
	requestUrl = "/role_modeling/insert";

	$("#modeling_dialog").dialog("open");
}

function editHandler() {
	let data = $("#modeling_list").datagrid("getSelected");

	if (data) {
		requestUrl = "/role_modeling/updateById";

		$("#id").val(data.id);
		$("#sex").combobox("setValue", data.sex);
		$("#name").textbox("setValue", data.name);
		$("#note").textbox("setValue", data.note);
		$("#phyle").combobox("setValue", data.phyle);
		$("#equipmentId").combobox("setValue", data.equipmentId);

		$("#modeling_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录。", "warning")
	}
}

function imageHandler() {
	let data = $("#modeling_list").datagrid("getSelected");

	if (data) {
		requestUrl = "/role_modeling/updateById";

		$("#modeling_id").val(data.id);
		$("#img").attr("src", data.image);

		$("#upload_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录。", "warning")
	}
}

$(document).ready(function() {

	$("#_phyle_").combobox({
		url: "/phyle/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		panelHeight: "auto",
		prompt: "--选择种族--"
	});

	$("#_sex_").combobox({
		url: "/gender/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		panelHeight: "auto",
		prompt: "--选择性别--"
	});

	$("#search").linkbutton({
		text: "搜索",
		iconCls: "icon-search"
	}).click(function() {
		let sex = $("#_sex_").combobox("getValue");
		let phyle = $("#_phyle_").combobox("getValue");

		$("#modeling_list").datagrid("load", {
			sex: sex,
			phyle: phyle
		});
	});

	$("#clear").linkbutton({
		text: "清空",
		iconCls: "icon-delete"
	}).click(function() {
		$("#search_form").form("clear");
	});

	$("#name").textbox({
		width: 120,
		required: true
	});
	
	$("#phyle").combobox({
		url: "/phyle/selectAll",
		valueField: "id",
		textField: "name",
		width: 120,
		required: true,
		panelHeight: "auto"
	});

	$("#sex").combobox({
		url: "/gender/selectAll",
		valueField: "id",
		textField: "name",
		width: 120,
		required: true,
		panelHeight: "auto"
	});

	$("#equipmentId").combobox({
		url: "/equipment_type/selectWeaponry",
		width: 120,
		valueField: "id",
		textField: "name",
		required: true,
		panelHeight: "auto",
		formatter: function(row) {
			return "<img alt='' width='20' src='" + row.image + "'/>&nbsp;" + row.name;
		}
	});
	
	$("#note").textbox({
		width: 320,
    	height: 100,
    	required: true,
		multiline: true
	});

	$("#modeling_dialog").dialog({
		title: "角色造型",
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#modeling_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(response) {
						showMsg(response.message);

						$(selector).form("clear");
						$("#modeling_dialog").dialog("close");
						$("#modeling_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#modeling_dialog").dialog("close");
				$("#modeling_form").form("clear");
			}
		}]
	});

	$("#upload").filebox({
		buttonText: "选择文件",
		width: 200,
		required: true,
		onChange: function() {
			let rowData = $("#modeling_list").datagrid("getSelected");

			if(rowData) {
				fileUpload(this, rowData.id, "/role_modeling/upload");
			}
		}
	});

	fetchUploadDialog(requestUrl, function () {
		$("#modeling_list").datagrid("reload");
	});

	// 兵器类型列表
	$("#bingqi_list").datagrid({
		url: "/equipment_category/select",
		method: "get",
		columns: [[
			{field: "name", title: "名称", align: "center", width: 100},
			{field: "image", title: "图片", align: "center", width: 60
				, formatter: function(value) {
					return "<img alt='' src='" + value + "' width='32' />";
				}
			},
			{field: "grade", title: "等级", align: "center", width: 60
				, formatter: function(value) {
					return "<span>" + value + "<span />级";
				}
			},
			{field: "note", title: "说明", align: "center", width: 560
				, formatter: function(value) {
					return "<div class='ell' title='" + value + "'>" + value + "</div>";
				}
			}
		]]
	});

	// 兵器详情对话框
	$("#bingqi_detail").dialog({
		title: "兵器详情",
		width: 800,
		height: 372
	});

	$("#modeling_list").datagrid({
		url: "/role_modeling/selectByPage",
		title: "角色造型列表",
		height: table_height_with_search,
		pagination: true,
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: '修改',
			handler: function() {
				editHandler();
			}
		}, "-", buttonUpload],
		columns: [[
			{field: "id", title: "编号", align: "center"},
			{field: "name", title: "角色名称", align: "center", width: 20},
			{field: "image", title: "图片", align: "center", width: 20
				, formatter: function(value) {
					return "<img alt='' width='" + size + "' src='" + value + "' />";
				}
			},
			{field: "sex", title: "性别", align: "center", width: 20
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/gender/selectById", {
						id: value
					}, function(resp) {
						let result = resp.data;

						$("#sex_" + rowIndex).attr("src", result.image)
							.attr('title', result.name);
					}, error);

					return "<img alt='' height='20' id='sex_" + rowIndex + "' />";
				}
			},
			{field: "phyle", title: "种族", align: "center", width: 20
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/phyle/selectById", {
						id: value
					}, function(resp) {
						let result = resp.data;

						$("#phyle_" + rowIndex).html(result.name);
					}, error);

					return "<div id='phyle_" + rowIndex + "'></div>";
				}
			},
			{field: "note", title: "角色介绍", align: "center", width: 100
				, formatter: function(value) {
					return "<div class='ell' title='" + value + "'>" + value + "</div>";
				}
			},
			{field: "equipmentId", title: "擅使兵器", align: "center", width: 20
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/equipment_type/selectNameById", {
						id: value
					}, function(response) {
						let name = response.data;

						$("#bingqi_" + rowIndex).html(name).click(function() {
							$("#bingqi_list").datagrid("reload", {
								typeId: value
							});
							
							$("#bingqi_detail").dialog({
								title: "兵器·" + name
							}).dialog("open");
						});
					}, error);
					
					return "<div class='blue' id='bingqi_" + rowIndex + "'></div>";
				}
			}
		]]
	});

});